<?php

namespace app\api\controller;

use app\admin\model\Sendwxmsg;

class Notes
{
    public $pagesize = 10;

    /**
     * 获取帖子列表接口
     */
    public function lists()
    {
        $auth = trim(input("request.auth"));//用户auth
        $ty = trim(input("request.ty"));//帖子类型 1话题 2梦话 ,ty=0表示获取所有帖子
        $hot = trim(input("hot"));//1获取推荐的 0获取全部的
        if (empty($hot)) {
            $hot = 0;
        } else {
            $hot = 1;
        }
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }

        $pagesize = trim(input("pagesize"));//每页条数

        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }

        $w = array();
        $w['is_delete'] = 0;//未删除
        $w['stats'] = 0;//已上架
        if ($hot) {
            $w['hot'] = $hot;
        }
        if (!empty($ty)) {
            $tyarray = explode(",", $ty);
            if (!empty($tyarray)) {
                $w['ty'] = array('in', $tyarray);
            }
        }

        $model = new \app\admin\model\Notes();
        $lists = $model->lists($w, $this->pagesize, "*", "sorts desc,id desc");
        $lists = $lists['data'];
        $lists = formateTimeForArr($lists, 'ctime');//获取发帖人信息
        $lists = changeUidToUserinfoForLists($lists, 'uid');//获取发帖人信息
        $lists = getNotesFilesForLists($lists, 0, "sorts desc,id asc");//批量获取帖子列表中的附件数据
        $lists = getNotesIsZanForLists($lists, $userInfo['id']);//批量获取帖子列表当前用户是否点赞
        $lists = getNotesIsGuanzhuForLists($lists, $userInfo['id']);//批量获取帖子列表当前用户是否关注发帖人
        $lists = filterHtmlForLists($lists);//过滤html标签

        //过滤举报的帖子接口
        $lists = $this->filterJubao($lists, $userInfo['id']);

        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $lists;
        return json($return);
    }

    /**
     *
     * 过滤举报的帖子
     */
    private function filterJubao($lists, $uid)
    {
        if (empty($lists)) {
            return array();
        }
        $tipModel = new \app\admin\model\Tip();
        $tipList = $tipModel->getDataForWheres(array('uid' => $uid));
        if (empty($tipList)) {
            return $lists;
        }
        $tipList = changeArrayKey($tipList, 'tid');
        $returnList = array();
        foreach ($lists as $key => $val) {
            if (empty($tipList[$val['id']])) {
                $returnList[] = $val;
            }
        }
        return $returnList;
    }

    /**
     * 获取我发布的帖子列表接口
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function myLists()
    {
        $auth = trim(input("request.auth"));//用户auth
        $ty = trim(input("request.ty"));//帖子类型 1话题 2梦话 ,ty=0表示获取所有帖子
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }

        $pagesize = trim(input("pagesize"));//每页条数

        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }

        $w = array();
        $w['uid'] = $userInfo['id'];//我的
        $w['is_delete'] = 0;//未删除

        if (!empty($ty)) {
            $tyarray = explode(",", $ty);
            if (!empty($tyarray)) {
                $w['ty'] = array('in', $tyarray);
            }
        }


        $model = new \app\admin\model\Notes();
        $lists = $model->lists($w, $this->pagesize);
        $lists = $lists['data'];
        $lists = formateTimeForArr($lists, 'ctime');//获取发帖人信息
        $lists = changeUidToUserinfoForLists($lists, 'uid');//获取发帖人信息
        $lists = getNotesFilesForLists($lists, 0, "sorts desc,id asc");//批量获取帖子列表中的附件数据
        $lists = getNotesIsZanForLists($lists, $userInfo['id']);//批量获取帖子列表当前用户是否点赞
        $lists = getNotesIsGuanzhuForLists($lists, $userInfo['id']);//批量获取帖子列表当前用户是否关注发帖人
        $lists = filterHtmlForLists($lists);//过滤html标签
        //过滤举报的帖子接口
        $lists = $this->filterJubao($lists, $userInfo['id']);
        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $lists;
        return json($return);
    }

    /**
     * 获取推荐的帖子列表接口
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function hotLists()
    {
        $auth = trim(input("request.auth"));//用户auth
        $ty = trim(input("request.ty"));//帖子类型 1话题 2梦话 ,ty=0表示获取所有帖子
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }

        $pagesize = trim(input("pagesize"));//每页条数

        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }

        $w = array();
        $w['hot'] = 1;//推荐的
        $w['is_delete'] = 0;//未删除
        $w['stats'] = 0;//已上架
        if (!empty($ty)) {
            $tyarray = explode(",", $ty);
            if (!empty($tyarray)) {
                $w['ty'] = array('in', $tyarray);
            }
        }


        $model = new \app\admin\model\Notes();
        $lists = $model->lists($w, $this->pagesize, "*", "sorts desc,id desc");
        $lists = $lists['data'];
        $lists = formateTimeForArr($lists, 'ctime');//获取发帖人信息
        $lists = changeUidToUserinfoForLists($lists, 'uid');//获取发帖人信息
        $lists = getNotesFilesForLists($lists, 0, "sorts desc,id asc");//批量获取帖子列表中的附件数据
        $lists = getNotesIsZanForLists($lists, $userInfo['id']);//批量获取帖子列表当前用户是否点赞
        $lists = getNotesIsGuanzhuForLists($lists, $userInfo['id']);//批量获取帖子列表当前用户是否关注发帖人
        $lists = filterHtmlForLists($lists);//过滤html标签
        //过滤举报的帖子接口
        $lists = $this->filterJubao($lists, $userInfo['id']);
        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $lists;
        return json($return);
    }

    /**
     * 获取一个帖子详情接口
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info()
    {
        $auth = trim(input("request.auth"));//用户auth
        $tid = trim(input("request.tid"));//帖子ID
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }

        if (empty($tid)) {
            $return = ['err' => 1, 'msg' => '获取失败', 'devMsg' => '获取失败，参数错误[tid]'];
            return json($return);
        }

        $model = new \app\admin\model\Notes();
        $info = $model->getID($tid);
        if (empty($info)) {
            $return = ['err' => 1, 'msg' => '获取失败', 'devMsg' => '获取失败，参数错误[tid]'];
            return json($return);
        }

        if ($info['stats']) {
            $return = ['err' => 1, 'msg' => '获取失败,帖子已经下架', 'devMsg' => '获取失败，帖子已经下架'];
            return json($return);
        }

        if ($info['is_delete']) {
            $return = ['err' => 1, 'msg' => '获取失败,帖子已经不存在', 'devMsg' => '获取失败，帖子已经不存在'];
            return json($return);
        }
        $info['ctime'] = formateDate($info['ctime']);
        $info['UserInfo'] = getUserInfoForID($info['uid']);//获取发帖人信息
        $info['notesFiles'] = getNotesFiles($info['id'], 0, "sorts desc,id asc");//获取发帖附件数据
        $info['isZan'] = getTidIsZan($userInfo['id'], $info['id']);

        $info['isGuanzhu'] = isFriend($userInfo['id'], $info['uid']);//

        //增加浏览数
        incNoteReadNum($tid);


        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $info;
        return json($return);
    }


    /**
     * 获取一个帖子详情接口
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function shareInfo()
    {
        $tid = trim(input("request.tid"));//帖子ID


        if (empty($tid)) {
            $return = ['err' => 1, 'msg' => '获取失败', 'devMsg' => '获取失败，参数错误[tid]'];
            return json($return);
        }
        $model = new \app\admin\model\Notes();
        $info = $model->getID($tid);
        if (empty($info)) {
            $return = ['err' => 1, 'msg' => '获取失败', 'devMsg' => '获取失败，参数错误[tid]'];
            return json($return);
        }

        if ($info['stats']) {
            $return = ['err' => 1, 'msg' => '获取失败,帖子已经下架', 'devMsg' => '获取失败，帖子已经下架'];
            return json($return);
        }

        if ($info['is_delete']) {
            $return = ['err' => 1, 'msg' => '获取失败,帖子已经不存在', 'devMsg' => '获取失败，帖子已经不存在'];
            return json($return);
        }
        $info['ctime'] = formateDate($info['ctime']);
        $info['UserInfo'] = getUserInfoForID($info['uid']);//获取发帖人信息
        $info['notesFiles'] = getNotesFiles($info['id'], 0, "sorts desc,id asc");//获取发帖附件数据

        //增加浏览数
        incNoteReadNum($tid);
        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $info;
        return json($return);
    }


    /**
     * 获取帖子回复列表接口
     */
    public function replyLists()
    {
        $auth = trim(input("request.auth"));//用户auth
        $tid = trim(input("request.tid"));//帖子ID
        $userInfo = checkLoginExp($auth);

        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }

        $pagesize = trim(input("pagesize"));//每页条数

        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }

        if (empty($tid)) {
            $return = ['err' => 1, 'msg' => '获取失败', 'devMsg' => '获取失败，参数错误[tid]'];
            return json($return);
        }


        $w = array();
        $w['is_delete'] = 0;//未删除
        $w['tid'] = $tid;
        $w['type'] = 0;//0回复 1点赞

        $model = new \app\admin\model\NotesReply();
        $lists = $model->lists($w, $this->pagesize);
        $lists = $lists['data'];
        $lists = formateTimeForArr($lists, 'ctime');//获取发帖人信息
        $lists = changeUidToUserinfoForLists($lists, 'uid', 'touid');//获取发帖人信息
        $lists = getNotesFilesForLists($lists, 1, "sorts desc,id asc");//批量获取帖子列表中的附件数据

        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $lists;
        return json($return);
    }


    /**
     * 获取帖子回复列表接口
     */
    public function shareReplyLists()
    {
        $tid = trim(input("request.tid"));//帖子ID
        $pagesize = trim(input("pagesize"));//每页条数

        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }

        if (empty($tid)) {
            $return = ['err' => 1, 'msg' => '获取失败', 'devMsg' => '获取失败，参数错误[tid]'];
            return json($return);
        }


        $w = array();
        $w['is_delete'] = 0;//未删除
        $w['tid'] = $tid;
        $w['type'] = 0;//0回复 1点赞

        $model = new \app\admin\model\NotesReply();
        $lists = $model->lists($w, $this->pagesize);
        $lists = $lists['data'];
        $lists = formateTimeForArr($lists, 'ctime');//获取发帖人信息
        $lists = changeUidToUserinfoForLists($lists, 'uid', 'touid');//获取发帖人信息
        $lists = getNotesFilesForLists($lists, 1, "sorts desc,id asc");//批量获取帖子列表中的附件数据

        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $lists;
        return json($return);
    }

    /**
     * 批量获取帖子回复列表接口
     */
    public function replyPlLists()
    {
        $auth = trim(input("request.auth"));//用户auth
        $tids = trim(input("request.tids"));//帖子ID
        $userInfo = checkLoginExp($auth);

        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }

        $pagesize = trim(input("pagesize"));//每页条数

        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }

        if (empty($tids)) {
            $return = ['err' => 1, 'msg' => '获取失败', 'devMsg' => '获取失败，参数错误[tids]'];
            return json($return);
        }

        $tempTidsArray = explode(",", $tids);
        if (empty($tempTidsArray)) {
            $return = ['err' => 1, 'msg' => '获取失败', 'devMsg' => '获取失败，参数错误[tids]'];
            return json($return);
        }

        $tidsArray = array();
        foreach ($tempTidsArray as $key => $val) {
            if ($val) {
                $tidsArray[] = $val;
            }
        }

        if (empty($tidsArray)) {
            $return = ['err' => 1, 'msg' => '获取失败', 'devMsg' => '获取失败，参数错误[tids]'];
            return json($return);
        }

        $resDataList = array();
        for ($i = 0; $i < count($tidsArray); $i++) {
            $w = array();
            $w['is_delete'] = 0;//未删除
            $w['tid'] = $tidsArray[$i];
            $w['type'] = 0;//0回复 1点赞

            $model = new \app\admin\model\NotesReply();
            $lists = $model->lists($w, $this->pagesize);
            $total = $lists['total'];
            $lists = $lists['data'];
            $lists = formateTimeForArr($lists, 'ctime');//获取发帖人信息
            $lists = changeUidToUserinfoForLists($lists, 'uid', 'touid');//获取发帖人信息
            $lists = getNotesFilesForLists($lists, 1, "sorts desc,id asc");//批量获取帖子列表中的附件数据
            $resDataList[$tidsArray[$i]] = array(
                'total' => $total,
                'lists' => $lists,
            );
        }


        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $resDataList;
        return json($return);
    }


    /**
     * 获取一个帖子的点赞用户列表接口
     */
    public function zanLists()
    {
        $auth = trim(input("request.auth"));//用户auth
        $tid = trim(input("request.tid"));//帖子ID
        $userInfo = checkLoginExp($auth);

        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }

        $pagesize = trim(input("pagesize"));//每页条数

        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }

        if (empty($tid)) {
            $return = ['err' => 1, 'msg' => '获取失败', 'devMsg' => '获取失败，参数错误[tid]'];
            return json($return);
        }


        $w = array();
        $w['is_delete'] = 0;//未删除
        $w['tid'] = $tid;
        $w['type'] = 1;//0回复 1点赞

        $model = new \app\admin\model\NotesReply();
        $lists = $model->lists($w, $this->pagesize);

        $total = $lists['total'];
        $lists = $lists['data'];
        $lists = formateTimeForArr($lists, 'ctime');//获取发帖人信息
        $lists = changeUidToUserinfoForLists($lists, 'uid');//获取发帖人信息
        $lists = getNotesFilesForLists($lists, 1, "sorts desc,id asc");//批量获取帖子列表中的附件数据


        $userList = array();
        foreach ($lists as $key => $val) {
            if ($val['UserInfo']) {
                $userList[] = $val['UserInfo'];
            }
        }
        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $userList;
        $return['total'] = $total;
        return json($return);
    }


    /**
     * 批量获取一推帖子的点赞用户列表接口 只取第一页
     */
    public function zanPlLists()
    {
        $auth = trim(input("request.auth"));//用户auth
        $tids = trim(input("request.tids"));//帖子ID  1,2,3
        $userInfo = checkLoginExp($auth);

        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }

        $pagesize = trim(input("pagesize"));//每页条数

        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }

        if (empty($tids)) {
            $return = ['err' => 1, 'msg' => '获取失败', 'devMsg' => '获取失败，参数错误[tids]'];
            return json($return);
        }

        $tempTidsArray = explode(",", $tids);
        if (empty($tempTidsArray)) {
            $return = ['err' => 1, 'msg' => '获取失败', 'devMsg' => '获取失败，参数错误[tids]'];
            return json($return);
        }

        $tidsArray = array();
        foreach ($tempTidsArray as $key => $val) {
            if ($val) {
                $tidsArray[] = $val;
            }
        }

        if (empty($tidsArray)) {
            $return = ['err' => 1, 'msg' => '获取失败', 'devMsg' => '获取失败，参数错误[tids]'];
            return json($return);
        }

        $resDataList = array();
        for ($i = 0; $i < count($tidsArray); $i++) {
            $w = array();
            $w['is_delete'] = 0;//未删除
            $w['tid'] = $tidsArray[$i];
            $w['type'] = 1;//0回复 1点赞
            $model = new \app\admin\model\NotesReply();
            $lists = $model->lists($w, $this->pagesize);
            $total = $lists['total'];
            $lists = $lists['data'];
            $lists = formateTimeForArr($lists, 'ctime');//获取发帖人信息
            $lists = changeUidToUserinfoForLists($lists, 'uid');//获取发帖人信息
            $lists = getNotesFilesForLists($lists, 1, "sorts desc,id asc");//批量获取帖子列表中的附件数据
            $userList = array();
            foreach ($lists as $key => $val) {
                if ($val['UserInfo']) {
                    $userList[] = $val['UserInfo'];
                }
            }
            $resDataList[$tidsArray[$i]] = array(
                'total' => $total,
                'list' => $userList
            );
        }


        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $resDataList;
        return json($return);
    }

    /**
     * 给帖子点赞接口
     */
    public function zan()
    {
        $auth = trim(input("request.auth"));//用户auth
        $tid = trim(input("request.tid"));//帖子ID
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,未登录', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }

        if (empty($tid)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[tid]'];
            return json($return);
        }

        $model = new \app\admin\model\Notes();
        $info = $model->getID($tid);
        if (empty($info)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[tid]'];
            return json($return);
        }

        if ($info['stats']) {
            $return = ['err' => 1, 'msg' => '操作失败,帖子已经下架', 'devMsg' => '操作失败，帖子已经下架'];
            return json($return);
        }

        if ($info['is_delete']) {
            $return = ['err' => 1, 'msg' => '操作失败,帖子已经不存在', 'devMsg' => '操作失败，帖子已经不存在'];
            return json($return);
        }


        $flag = getTidIsZan($userInfo['id'], $tid);//0未点赞 1已经点赞
        if ($flag) {
            $return = ['err' => 1, 'msg' => '操作失败,您已经对该帖子点赞', 'devMsg' => '操作失败，您已经对该帖子点赞'];
            return json($return);
        }
        if (notesZan($userInfo['id'], $tid)) {
            $return['err'] = 0;
            $return['msg'] = '操作成功';
            return json($return);
        } else {
            $return['err'] = 1;
            $return['msg'] = '操作失败';
            return json($return);
        }

    }

    /**
     * 发帖接口
     * @return \think\response\Json
     */
    public function pub()
    {
        $auth = trim(input("request.auth"));//用户auth
        $name = trim(input("request.name"));//贴名称
        $content = trim(input("request.content"));//帖子内容
        $ty = trim(input("request.ty"));//帖子类型 1话题 2梦话 3想法

        $img = trim(input("request.img"));//帖子类型是梦话的时候，梦话的背景图
        $json_images = trim(input("request.json_images"));//json字符串
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,未登录', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }

        if (empty($ty)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[ty]'];
            return json($return);
        }

        if (empty($content)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[content]'];
            return json($return);
        }

        if (empty($img)) {
            $json_imagesArray = json_decode($json_images, true);
            if (!empty($json_imagesArray)) {
                $img = $json_imagesArray[0];
            }
        }

        $model = new \app\admin\model\Notes();
        $nodeData = array(
            'name' => $name,
            'content' => $content,
            'name' => $name,
            'img' => $img,
            'ctime' => time(),
            'uid' => $userInfo['id'],
            'stats' => 0,//0上架 1下架
            'hot' => 0,//0普通，1推荐
            'is_delete' => 0,// 0否 1是
            'ty' => $ty,
        );
        $insertId = $model->add($nodeData);
        if ($insertId) {
            $this->addNotesFiles($insertId, $json_images, 0);
            $return['err'] = 0;
            $return['msg'] = '操作成功';
            return json($return);
        } else {
            $return['err'] = 1;
            $return['msg'] = '操作失败';
            return json($return);
        }
    }


    /**
     * 回复接口
     * @return \think\response\Json
     */
    public function replay()
    {
        $auth = trim(input("request.auth"));//用户auth
        $content = trim(input("request.content"));//帖子内容
        $tid = trim(input("request.tid"));//帖子ID
        $touid = trim(input("request.touid"));//回复的哪个用户的回复
        $json_images = trim(input("request.json_images"));//json字符串
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,未登录', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }

        if (empty($tid)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[tid]'];
            return json($return);
        }

        if (empty($content)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[content]'];
            return json($return);
        }
        if (empty($touid)) {
            $touid = 0;
        }


        $model = new \app\admin\model\NotesReply();
        $nodeData = array(
            'tid' => $tid,
            'type' => 0,
            'content' => $content,
            'ctime' => time(),
            'uid' => $userInfo['id'],
            'touid' => $touid,
            'is_delete' => 0,// 0否 1是
        );
        $insertId = $model->add($nodeData);
        if ($insertId) {
            $this->addNotesFiles($insertId, $json_images, 1);
            //增加帖子回复数量
            incNoteReplyNum($tid);
            $return['err'] = 0;
            $return['msg'] = '操作成功';
            return json($return);
        } else {
            $return['err'] = 1;
            $return['msg'] = '操作失败';
            return json($return);
        }
    }


    private function addNotesFiles($tid, $json_images, $typeid = 0)
    {
        if (empty($tid)) {
            return false;
        }
        if (empty($json_images)) {
            return false;
        }
        $json_images = json_decode($json_images, true);
        if (empty($json_images)) {
            return false;
        }

        $insertData = array();
        foreach ($json_images as $key => $val) {
            $ty = 1;
            $suffix = getSuffixForFile($val['file']);
            if (in_array($suffix, ['jpg', 'jpeg', 'png'])) {
                $ty = 1;
            } else if (in_array($suffix, ['mp3', 'mp4', 'avi', 'wmv', 'flv'])) {
                $ty = 2;
            } else {
                $ty = 3;
            }

            $tempData = array(
                'tid' => $tid,
                'file' => $val['file'],
                'thumb' => $val['thumb'],
                'ctime' => time(),
                'ty' => $ty,
                'typeid' => $typeid,
            );
            $insertData[] = $tempData;
        }
        if (empty($insertData)) {
            return false;
        }
        $model = new \app\admin\model\NotesFiles();
        $model->addAll($insertData);
    }

    function defaultDreamBgLists()
    {
        $lists = config("DreamDefaultBackgroundImages");
        $return['err'] = 0;
        $return['msg'] = '操作成功';
        $return['data'] = $lists;
        return json($return);
    }


    /**
     * 统计当前用户 发帖动态数量  关注的人数  粉丝人数
     * @return \think\response\Json
     */
    public function statistics()
    {
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }
        $resData = array(
            'fans' => 0,
            'guanzhu' => 0,
            'dongtai' => 0,
        );
        $model = new \app\admin\model\UserFriend();
        //获取粉丝数量
        $sql = "select count(*) as num from t_user_friend where fuid = '{$userInfo['id']}'";
        $result = $model->querySelectSQL($sql);
        if ($result[0]['num']) {
            $resData['fans'] = $result[0]['num'];
        }
        //获取我关注的数量
        $sql = "select count(*) as num from t_user_friend where uid = '{$userInfo['id']}'";
        $result = $model->querySelectSQL($sql);
        if ($result[0]['num']) {
            $resData['guanzhu'] = $result[0]['num'];
        }
        //获取我发帖数
        $sql = "select count(*) as num from t_notes where uid = '{$userInfo['id']}' and is_delete=0";
        $result = $model->querySelectSQL($sql);
        if ($result[0]['num']) {
            $resData['dongtai'] = $result[0]['num'];
        }


        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $resData;
        return json($return);

    }


    /**
     * 统计当前用户 发帖动态数量  关注的人数  粉丝人数
     * @return \think\response\Json
     */
    public function del()
    {
        $auth = trim(input("request.auth"));//用户auth
        $tid = trim(input("request.tid"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,未登录', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }
        if (empty($tid)) {
            $return = ['err' => 1, 'msg' => '操作失败,参数错误', 'devMsg' => '操作失败，参数错误[tid]'];
            return json($return);
        }
        $model = new \app\admin\model\Notes();
        $info = $model->getID($tid);
        if (empty($info)) {
            $return = ['err' => 1, 'msg' => '操作失败，记录不存在', 'devMsg' => '操作失败，参数错误[tid]'];
            return json($return);
        }

        if ($info['is_delete']) {
            $return = ['err' => 1, 'msg' => '操作失败,帖子已经删除', 'devMsg' => '操作失败，帖子已经删除'];
            return json($return);
        }

        if ($info['uid'] != $userInfo['id']) {
            $return = ['err' => 1, 'msg' => '操作失败,您无权操作该记录', 'devMsg' => '操作失败，不是自己发的帖子'];
            return json($return);
        }

        $res = $model->updateData(array('is_delete' => 1), $tid);
        if ($res || $res == 0) {
            $return['err'] = 0;
            $return['msg'] = '操作成功';
            return json($return);
        } else {
            $return['err'] = 1;
            $return['msg'] = '操作成功';
            return json($return);
        }
    }


    /**
     * 举报接口
     */
    public function tip()
    {
        $auth = trim(input("request.auth"));//用户auth
        $tid = trim(input("request.tid"));//用户auth
        $content = trim(input("request.content"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,未登录', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }
        if (empty($tid)) {
            $return = ['err' => 1, 'msg' => '操作失败,文章ID为空', 'devMsg' => '操作失败，参数错误[tid]'];
            return json($return);
        }

        $model = new \app\admin\model\Tip();
        $newData = array(
            'tid' => $tid,
            'content' => $content,
            'uid' => $userInfo['id'],
            'ctime' => time()
        );

        $res = $model->add($newData);
        if ($res) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }
    }
}
